home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr47 / 131_01.zip / FPUT.C < prev    next >
Text File  |  1993-06-05  |  6KB  |  341 lines

  1. /*
  2.     ****************************************************
  3.             FPUT Copy Utility
  4.     ****************************************************
  5.  
  6.     W. Lemiszki                 24 Oct 1983
  7.  
  8.     Filename: arglist.c             BDS C v1.50
  9.  
  10.     Copies a list of files into a destination user/drive.
  11.  */
  12.  
  13. #include <bdscio.h>            /* Standard Definitions */
  14.  
  15. char dstdrive[20];            /* destination user/dr:        */
  16. char *memory;                /* ptr to free memory         */
  17. unsigned msects;            /* size of memory buffer    */
  18.  
  19.  
  20.  
  21. /*
  22.  *    M A I N
  23.  *    -------
  24.  */
  25. main(argc, argv)
  26. int argc;  char *argv[];
  27. {
  28.     char lbuf[MAXLINE];            /* argument line buffer    */
  29.     char *gets();
  30.  
  31.     puts("FPUT  v2.1\n\n");
  32.     if (argc < 3)
  33.         {
  34.         puts("\7Usage: fput [u/]d: <ufn> @<ufn> ...\n");
  35.         exit();
  36.         }
  37.  
  38.     if (get_drive(argv[1]) == ERROR)    /* scan dest. drive */
  39.         {
  40.         puts("\7Bad user/drive: argument.\n");
  41.         exit();
  42.         }
  43.  
  44.     memory = sbrk(128);            /* get 1st chunk of memory */
  45.     msects = 1;
  46.     while (sbrk(128) != ERROR)        /* allocate it all */
  47.         ++msects;
  48.  
  49.     proclist(argc-2, argv+2);        /* process the cmd args */
  50. }
  51.  
  52.  
  53.  
  54.  
  55. /*
  56.  *    Process a list of args
  57.  *    ----------------------
  58.  */
  59. proclist(argc, argv)
  60. int argc;  char *argv[];
  61. {
  62.     int i;
  63.  
  64.     for (i=0; i<argc; ++i)            /* do each arg */
  65.         if (argv[i][0] == '@')
  66.             procfile(++argv[i]);    /* get from file */
  67.             else
  68.             fput(argv[i]);        /* do it */
  69. }
  70.  
  71.  
  72.  
  73. /*
  74.  *    Process arguments from a file
  75.  *    -----------------------------
  76.  */
  77. procfile(arg)
  78. char *arg;
  79. {
  80.     char fname[20], iobuf[BUFSIZ], lbuf[MAXLINE];
  81.  
  82.     if (arg[0] == EOS)            /* default ? */
  83.         strcpy(fname, "FILES.CAT");
  84.         else
  85.         strcpy(fname, arg);
  86.     if (index(fname, '.') == NULL)        /* if no extention */
  87.         strcat(fname, ".CAT");
  88.  
  89.     putchar('@');                /* echo name */
  90.     putlj(fname, 17);
  91.  
  92.     if (fopen(fname, iobuf) == ERROR)
  93.         return err(" does not exist.");
  94.     putchar('\n');
  95.  
  96.     msects -= 10;                /* leave stk space for iobuf */
  97.     while (fgets(lbuf, iobuf) != NULL)    /* until EOF */
  98.         procline(lbuf);
  99.     msects += 10;                /* restore memory */
  100.  
  101.     putchar('\n');
  102.     fclose(iobuf);
  103. }
  104.  
  105.  
  106.  
  107. /*
  108.  *    Process a line of arguments
  109.  *    ---------------------------
  110.  */
  111. procline(line)
  112. char *line;
  113. {
  114.     int argc;  char *argv[20];
  115.     char *ptr;
  116.  
  117.     for (ptr=line;  *ptr = toupper(*ptr);  ++ptr)
  118.         if (*ptr == ';'  ||  *ptr == '\n')    /* comment ? */
  119.             break;
  120.     *ptr = EOS;                    /* terminate */
  121.     argc = parseargs(line, argv);
  122.     proclist(argc, argv);
  123. }
  124.  
  125.  
  126. /* Parse a string up into args */
  127. int parseargs(str, argv)
  128. char *str, *argv[];
  129. {
  130.     int argc;
  131.     char d1, d2;            /* delimiters */
  132.     char c;
  133.  
  134.     argc = 0;
  135.     do
  136.         {
  137.         while ((c = *str) == ' '  ||  c == '\t')
  138.             ++str;                /* skip whitespace */
  139.  
  140.         if (c == '"'  ||  c == '\'')        /* quoted string ? */
  141.             {
  142.             d1 = d2 = c;
  143.             ++str;                /* skip quote */
  144.             }
  145.             else
  146.             {
  147.             d1 = ' ';
  148.             d2 = '\t';
  149.             }
  150.  
  151.         if (c)                    /* != EOS */
  152.             argv[argc++] = str;        /* save ptr */
  153.  
  154.         while ((c = *str)  &&  c != d1  &&  c != d2)
  155.             ++str;            /* scan til delim or EOS */
  156.  
  157.         *str++ = EOS;            /* terminate arg */
  158.         } while (c);            /* scan entire string */
  159.  
  160.     return argc;
  161. }
  162.  
  163.  
  164.  
  165.  
  166. /* Print an error msg */
  167. int err(msg)
  168. char *msg;
  169. {
  170.     puts(msg);
  171.     puts("\7\n");        /* terminate and beep */
  172.     return ERROR;
  173. }
  174.  
  175.  
  176. /* Put a left justified string */
  177. putlj(str, width)
  178. char *str;
  179. int width;
  180. {
  181.     while (width--)
  182.         if (*str)
  183.             putchar(*str++);
  184.             else
  185.             putchar(' ');
  186. }
  187.  
  188.  
  189. /* Check for wildcard characters */
  190. haswild(arg)
  191. char *arg;
  192. {
  193.     return ((index(arg, '?') || index(arg,'*'))  ?  ERROR  :  OK);
  194. }
  195.  
  196.  
  197. /* Find a char in a string */
  198. char *index(str, c)
  199. char *str, c;
  200. {
  201.     while (*str)
  202.         {
  203.         if (c == *str)
  204.             return (str);
  205.         ++str;
  206.         }
  207.     return NULL;
  208. }
  209.  
  210.  
  211.  
  212.  
  213. /*
  214.  *    Scan the destination user/drive
  215.  *    -------------------------------
  216.  *    (Only looks at last char.)
  217.  */
  218. int get_drive(arg)
  219. char *arg;
  220. {
  221.     char c;
  222.  
  223.     strcpy(dstdrive, arg);            /* move into buffer */
  224.     if ((c = arg[strlen(arg) - 1]) == '/'  ||  (c == ':'))
  225.         return OK;
  226.     return ERROR;
  227. }
  228.  
  229.  
  230.  
  231.  
  232. /*
  233.  *    Copy a file to 'dstdrive'
  234.  *    ------------------------
  235.  */
  236. fput(srcname)
  237. char *srcname;                /* source filename */
  238. {
  239.     char dstname[20];            /* dst filename buf */
  240.     char *ptr;
  241.  
  242.     putlj(srcname, 18);            /* print the arg */
  243.     if (haswild(srcname))
  244.         return (err("is ambiguous."));
  245.  
  246.     ptr = srcname;                /* make dstname from srcname */
  247.     if (index(ptr, '/'))            /* != NULL */
  248.         ptr = index(ptr, '/');        /* drop user number */
  249.     if (index(ptr, ':'))            /* != NULL */
  250.         ptr = index(ptr, ':');        /* drop drive spec */
  251.  
  252.     strcpy(dstname, dstdrive);        /* init with drive spec. */
  253.     strcat(dstname, ptr);            /* add base filename */
  254.     fcopy(srcname, dstname);        /* copy the file */
  255. }
  256.  
  257.  
  258.  
  259.  
  260. /*
  261.  *    Copy 'srcname' to 'dstname'
  262.  *    ---------------------------
  263.  *    Uses a temporary output file, and renames only after
  264.  *    successful completion.
  265.  */
  266. int fcopy(srcname, dstname)
  267. char *srcname, *dstname;
  268. {
  269.     char tmpname[20];            /* temporary output filename */
  270.     int srcfd, dstfd;            /* file descriptors */
  271.  
  272.     maketemp(dstname, tmpname);        /* generate temp filename */
  273.  
  274.     if ((srcfd = open(srcname, 0)) == ERROR)
  275.         return (err("does not exist."));
  276.  
  277.     if ((dstfd = creat(tmpname)) == ERROR)
  278.         {
  279.         close(srcfd);
  280.         return (err("opened, but can't create temporary."));
  281.         }
  282.  
  283.     if (fcpy(srcfd, dstfd) == ERROR)    /* move the data */
  284.         {
  285.         close(srcfd);
  286.         fabort(dstfd);
  287.         return ERROR;
  288.         }
  289.  
  290.     close(srcfd);
  291.     close(dstfd);
  292.     unlink(dstname);
  293.     if (rename(tmpname, dstname) == ERROR)
  294.         {
  295.         unlink(tmpname);
  296.         return (err("copied, but can't rename temporary."));
  297.         }
  298.  
  299.     puts("copied to ");
  300.     puts(dstname);
  301.     putchar('\n');
  302.     return (OK);
  303. }
  304.  
  305.  
  306.  
  307.  
  308. /* Make a temporary filename */
  309. maketemp(name, buf)
  310. char *name;                /* base name */
  311. char *buf;                /* destination buffer */
  312. {
  313.     char *index();
  314.  
  315.     strcpy(buf, name);            /* name to buffer */
  316.     if (index(buf, '.'))            /* != NULL */
  317.         *index(buf, '.') = EOS;        /* drop the extension */
  318.     strcat(buf, ".$F$");            /* add temp ext */
  319. }
  320.  
  321.  
  322.  
  323. /* Transfer Data */
  324. int fcpy(srcfd, dstfd)
  325. int srcfd, dstfd;
  326. {
  327.     int n;
  328.  
  329.     while (n = read(srcfd, memory, msects))        /* 0 is EOF */
  330.         {
  331.         if (n == ERROR)
  332.             return (err("contains unreadable data."));
  333.         if (write(dstfd, memory, n) != n)
  334.             return (err("ok, but error writing. (disk full?)"));
  335.         }
  336.     return (OK);
  337. }
  338.  
  339.  
  340. /*EOF*/
  341.